package com.lanou.musicproject.adapter;

import android.app.ActivityManager;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.lanou.musicproject.R;
import com.lanou.musicproject.activity.MainActivity;
import com.lanou.musicproject.activity.MusicPlayActivity;
import com.lanou.musicproject.bean.MyMusicList;
import com.lanou.musicproject.bean.RotateInfo;
import com.lanou.musicproject.suspendservice.FxService;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by 王世美 on 15/10/28.
 * 轮播内容
 */
public class RotateInfoAdapter extends BaseAdapter {
    // 定义一个实体类数组
    private List<RotateInfo.ContentEntity> content;
    private boolean isService = false;
    private RotateInfo info;
    // 上下文
    private Context context;

    //有参构造方法
    public RotateInfoAdapter(Context c, List<RotateInfo.ContentEntity> content) {
        this.content = content;
        context = c;
    }

    //获得行布局个数
    @Override
    public int getCount() {
        if (content != null && content.size() > 0)
            return content.size();
        else
            return 0;
    }

    // 获得行布局坐标
    @Override
    public RotateInfo.ContentEntity getItem(int position) {
        if (content != null && content.size() > 0)
            return content.get(position);
        else
            return null;
    }

    // 获得行布局内容Id
    @Override
    public long getItemId(int position) {
        return position;
    }

    // 获得行布局内容
    @Override
    public View getView(final int position,
                        View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.rotate_info_listview_item, null);
            holder = new ViewHolder();
            holder.rorateTitleTV = (TextView) convertView.findViewById(R.id.rorateTitleTV);
            holder.rorateAuthorTV = (TextView) convertView.findViewById(R.id.rorateAuthorTV);
            holder.rotate_listView = (LinearLayout) convertView.findViewById(R.id.rotate_listView);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        RotateInfo.ContentEntity contentEntity = getItem(position);
        if (contentEntity != null) {
            holder.rorateTitleTV.setText(contentEntity.getTitle());
            holder.rorateAuthorTV.setText(contentEntity.getAuthor());
            //遍历音乐播放
            holder.rotate_listView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    ArrayList<MyMusicList> lists = new ArrayList<>();
                    for (RotateInfo.ContentEntity entity : content) {
                        MyMusicList musicList = new MyMusicList();
                        musicList.setName(entity.getTitle());
                        musicList.setSinger(entity.getAuthor());
                        musicList.setSongId(entity.getSong_id());
                        musicList.setFrom("网络");
                        lists.add(musicList);
                    }
                    MainActivity.mini_play(lists, position);

                        FxService.mini_play(lists, position);
                    MusicPlayActivity.playIv(lists,position);

                }
            });
        }
        return convertView;
    }

    // 初始化组件
    private class ViewHolder {
        public TextView rorateTitleTV;
        public TextView rorateAuthorTV;
        public LinearLayout rotate_listView;
    }

    public boolean IsService(Context mContext, String serviceName) {
        ActivityManager myAM = (ActivityManager) mContext
                .getSystemService(Context.ACTIVITY_SERVICE);
        List<ActivityManager.RunningServiceInfo> myList = myAM.getRunningServices(40);
        if (myList.size() <= 0) {
            return false;
        }
        for (int i = 0; i < myList.size(); i++) {
            String mName = myList.get(i).service.getClassName().toString();
            if (mName.equals("com.lanou.musicproject.suspendservice.FxService")) {
                isService = true;
                break;
            }
        }
        return isService;

    }

}
